package com.itheima.controller.admin;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.common.utils.AliOssUtil;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import freemarker.template.Template;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import freemarker.template.Configuration;

import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.Set;
import java.util.UUID;

@RestController
@RequestMapping("/setmeal")
@Api(tags = "套餐相关接口")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private AliOssUtil util;
    @Qualifier("redisTemplate")
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private Configuration configuration;


    /**
     * 新增套餐
     * @param setmeal
     * @param checkgroupIds
     * @return
     */
    @ApiOperation("新增套餐")
    @PostMapping("/add")
    public Result add(@RequestBody Setmeal setmeal,String checkgroupIds)throws Exception{
        log.info("新增套餐:{},{}",setmeal,checkgroupIds);
        setmealService.add(setmeal,checkgroupIds);

        return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS,null);
    }

    /**
     * 修改套餐
     * @param setmeal
     * @param checkGroupIds
     * @return
     */
    @ApiOperation("修改套餐")
    @PostMapping("/edit")
    public Result edit(@RequestBody Setmeal setmeal,String checkGroupIds){
        log.info("修改套餐:{},{}",setmeal,checkGroupIds);
        setmealService.edit(setmeal,checkGroupIds);
        return new Result(true, MessageConstant.EDIT_SETMEAL_SUCCESS,null);
    }

    /**
     * 根据id查询套餐数据
     * @param id
     * @return
     */
    @GetMapping("findById")
    @ApiOperation("根据id查询套餐数据")
    public Result findById(Integer id){
        log.info("根据id查询套餐数据");
        Setmeal result = setmealService.findById(id);
        return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,result);
    }

    /**
     * 分页查询套餐
     * @param pageBean
     * @return
     */
    @PostMapping("findPage")
    @ApiOperation("分页查询套餐")
    public PageResult findPage(@RequestBody QueryPageBean pageBean)throws Exception{
        log.info("分页查询套餐:{}",pageBean);
        PageResult result = setmealService.findPage(pageBean);
        return result;
    }

    /**
     * 图片上传
     * @param imgFile
     * @return
     */
    @PostMapping("/upload")
    @ApiOperation("图片上传")
    public Result upload(MultipartFile imgFile){
        MultipartFile file = imgFile;
        log.info("图片上传:{}",file);

        try {
            //截取原始文件名
            String originalFilename = file.getOriginalFilename();

            //截取原始文件名后缀
            String extension = originalFilename.substring(originalFilename.lastIndexOf("."));

            //获取UUID命名
            String ObjectName = UUID.randomUUID().toString() + extension;

            //上传文件
            String filePath = util.upload(file.getBytes(), ObjectName);

            //将文件上传到redis中
            SetOperations setOperation =  redisTemplate.opsForSet();


            //传入原始文件
            if(setOperation.members("origin_") != null && setOperation.members("origin_").size() != 0) {
                Set origin = setOperation.members("origin_");
                for (Object o : origin) {
                    setOperation.remove("origin_",o);
                }
            }
            setOperation.add("origin_",filePath);

            setOperation.add("upload_",filePath);

            return new Result(true,MessageConstant.PIC_UPLOAD_SUCCESS,filePath);
        } catch (Exception e) {
            log.error("文件上传失败:{}",e);
        }

        return new Result(false,MessageConstant.PIC_UPLOAD_FAIL,null);
    }


    /**
     * 获取所有套餐信息
     * @return
     */
    @GetMapping("/getSetmeal")
    @ApiOperation("获取所有套餐信息")
    public Result getSetmeal() throws Exception {
        log.info("获取所有套餐数据");
        List<Setmeal> list = setmealService.getSetmeal();
        return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,list);
    }

    /**
     * 根据id查询套餐详情
     * @param id
     * @return
     */
    @PostMapping("findById")
    @ApiOperation("根据id查询套餐详情")
    public Result findSetmealById(Integer id){
        log.info("根据id查询套餐详情");
        Setmeal result = setmealService.findById(id);
        return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,result);
    }
}
